<template>
  <div class="announcement-detail">
    <el-form class="form-wrap" label-position="top" label-width="80px" :model="formData">
      <el-form-item :label="$t('configLazyLoad.process.nodeName')">
        <el-input v-model.trim="formData.DisplayName"></el-input>
      </el-form-item>
      <el-form-item label="语音文件：">
        <el-select v-model="formData.Filename" placeholder="请选择" style="width: 100%;" filterable>
          <el-option
            label="请选择"
            value="">
          </el-option>
          <el-option
            v-for="(item,index) in ivr_sound.ivr_sound"
            :key="index"
            v-if="item.SoundType == 'normal'"
            :label="item.DisplayName"
            :value="item.Filename">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <span style="font-size: 12px;color: #000;margin-right: 16px;">播放过程中不可打断：</span>
        <el-radio-group v-model="formData.NotBreak">
          <el-radio :label="true">是</el-radio>
          <el-radio :label="false">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="播放结束后转向：">
        <node-select-item ref="nodeSelect" :curPbx="curPbx" :ToPeerType="form.ToPeerType" :ToPeer="form.ToPeer"></node-select-item>
      </el-form-item>
    </el-form>
    <div class="footer">
      <el-button class='slide-box__button' @click='cancelClick'>
        取消
      </el-button>
      <el-button class='slide-box__button' type="primary" @click='saveClick'>
        保存
      </el-button>
    </div>
  </div>
</template>
<script>
  import nodeSelectItem from '../../base/nodeSelectItem.vue'
  export default {
    name: 'mainIvrAnnouncementNodeLink',
    data () {
      return {
        formData: {
          _id: '',
          DisplayName: '',
          Filename: '',
          NotBreak: false,
          ToPeerType: '',
          ToPeer: ''
        }
      }
    },
    props: {
      form: {
        type: Object,
        default: function () {
          return {}
        }
      }
    },
    components: {
      nodeSelectItem
    },
    computed: {
      Account () {
        return this.$store.state.session.account.account
      },
      curPbx () {
        return this.$store.state.config.callCenter.currentPBX
      },
      ivr_sound () {
        return this.$store.state.session.ivrMap[this.curPbx].data
      }
    },
    methods: {
      init () {
        this.formData._id = this.form._id
        if (this.form.Exten) {
          this.formData.Exten = this.form.Exten
        }
        this.formData.DisplayName = this.form.DisplayName
        let flag = false
        if (this.ivr_sound.ivr_sound && this.ivr_sound.ivr_sound.length > 0) {
          this.ivr_sound.ivr_sound.forEach(item => {
            if (this.form.Filename === item.Filename) {
              flag = true
            }
          })
        }
        if (!flag) {
          this.formData.Filename = ''
        } else {
          this.formData.Filename = this.form.Filename
        }
        this.formData.NotBreak = this.form.NotBreak
      },
      cancelClick () {
        this.$emit('cancelClick')
      },
      saveClick () {
        if (!this.formData.DisplayName) {
          this.$message({
            type: 'error',
            message: '节点名称不能为空!'
          })
          return
        }
        this.formData.ToPeerType = this.$refs.nodeSelect.ToPeerTypeThis
        this.formData.ToPeer = this.$refs.nodeSelect.ToPeerThis
        let obj = this.formData
        obj.PBX = this.curPbx
        obj.Account = this.Account
        this.$store.dispatch('updateAnnouncement', obj).then(res => {
          if (res && res.success) {
            this.$message({
              type: 'success',
              message: '保存成功!'
            })
            this.updateVuexPbx()
          }
        })
      },
      updateVuexPbx () {
        this.$store.dispatch('queryAnnouncement', {Account: this.Account, PBX: this.curPbx}).then(resp => {
          if (resp && resp.success) {
            let listData = []
            resp.list.forEach(one => {
              if (!one.ServiceNo) {
                listData.push(one)
              }
            })
            this.$store.commit('SET_IVRMAP_DETAIL', {PBX: this.curPbx, typeName: 'ivr_node_announcement', data: listData || [], flag: 'data', node: this})
          }
          this.$emit('saveClick')
        })
      }
    },
    watch: {
      form: {
        deep: true,
        handler (nv, ov) {
          this.init()
          this.$refs.nodeSelect.init()
        }
      }
    },
    beforeMount () {
      this.init()
    }
  }
</script>

<style lang="stylus" scoped>
  .announcement-detail
    position relative
    width 100%
    height 100%
    .form-wrap
      padding 20px
  .footer
    position absolute
    bottom 0
    left 0
    width 100%
    background rgba(255,255,255,1)
    box-shadow 0px -2px 4px 0px rgba(5,49,38,0.05)
    border-top 1px solid rgba(232,232,232,1)
    display flex
    justify-content center
    padding 13px 0
    box-sizing border-box
    .slide-box__button
      padding 8px 20px 8px
      box-sizing border-box
</style>
